package com.wanban.txplayertest

import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade
import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.RoundedCornersTransformation


object ImageUtils {

    private val defaultVResId = R.drawable.hyys_bg_default_v

    private val defaultHResId = R.drawable.hyys_bg_default_h

    fun loadImage(view: ImageView?, url: Any?) {
        if (view == null || view.context == null) {
            return
        }
        try {
            GlideApp.with(view.context)
                .load(url)
                .error(defaultVResId)
                .apply(
                    RequestOptions.bitmapTransform(
                        MultiTransformation(
                            CenterCrop(), RoundedCornersTransformation(8, 0, RoundedCornersTransformation.CornerType.ALL)
                        )
                    )
                )
                .transition(withCrossFade(300))
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .into(view)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }


    fun loadBannerImage(view: ImageView?, url: Any?) {
        if (view == null || view.context == null) {
            return
        }
        try {
            GlideApp.with(view.context)
                .load(url)
                .error(defaultHResId)
                .apply(
                    RequestOptions.bitmapTransform(
                        MultiTransformation(
                            CenterCrop(), RoundedCornersTransformation(8, 0, RoundedCornersTransformation.CornerType.ALL)
                        )
                    )
                )
                .transition(withCrossFade(300))
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .into(view)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }


    fun stopLoadImage(view: ImageView) {
        Glide.with(view.context).clear(view)
    }

    fun clearMemory() {
        Glide.get(MyApplication.context).clearMemory()
    }


}